import { Component, Label, Node, _decorator, color } from "cc"
import BaseDialog from "./BaseDialog"
import MovieClip from "../common/MovieClip"
const {ccclass,property} = _decorator
const ASK_COLOR = color('#0099FF')
const WARN_COLOR = color('#EB5444')
type ToastData = {
    title:string,
    content:string,
    /**
     * Toast类型:warn、ask、save
     */
    type:string,
    btOk?:string,
    btCancel?:string,
    btSure?:string,
    showClose?:boolean
}
@ccclass
export default class ToastDialog extends BaseDialog {
    @property(Label)
    private titleLabel:Label = null
    @property(Label)
    private contentLabel:Label = null
    @property(Component)
    private bt_cancel:Component = null
    @property(Component)
    private bt_confirm:Component = null
    @property(Component)
    private bt_ok:Component = null
    @property(Component)
    private bt_close:Component = null
    @property(Label)
    private bt_ok_label:Label = null
    //data:ToastData
    private _dataSource:ToastData
    set dataSource(data:ToastData) {
        this._dataSource = data
        if(this.node.active)
        this.flushUI()
    }
    protected onLoad(): void {
        if(this._dataSource){
            this.flushUI()
        }
    }
    private flushUI(): void {
        if(this._dataSource.type=='ask'){
            this.titleLabel.color=ASK_COLOR
            this.bt_cancel.node.active=true
            this.gotoAndStop(this.bt_cancel.node)
            this.bt_confirm.node.active=true
            this.gotoAndStop(this.bt_confirm.node)
            this.bt_ok.node.active=false
        }else if(this._dataSource.type=='warn'){
            this.titleLabel.color=WARN_COLOR
            this.bt_cancel.node.active=false
            this.bt_confirm.node.active=false
            this.bt_ok.node.active=true
        }else if(this._dataSource.type=='save'){
            this.titleLabel.color=WARN_COLOR
            this.bt_cancel.node.active=true
            this.gotoAndStop(this.bt_cancel.node,1)
            this.bt_confirm.node.active=true
            this.gotoAndStop(this.bt_confirm.node,1)
            this.bt_ok.node.active=false
        }
        this.titleLabel.string=this._dataSource.title
        this.contentLabel.string=this._dataSource.content
        if(this._dataSource.btOk!=undefined){
            this.bt_ok_label.string=this._dataSource.btOk
        }else{
            this.bt_ok_label.string='我知道了'
        }
        //关闭按钮
        if(this._dataSource.showClose!=undefined&&this._dataSource.showClose==false){
            this.bt_close.node.active=false
        }else{
            this.bt_close.node.active=true
        }
    }
    private onSure():void{
        this.onClose()
        this.successCallback&&this.successCallback()
    }
    private onCancel():void{
        this.onClose()
        this.failCallback&&this.failCallback()
    }
    private gotoAndStop(bt:Node,frame=0):void{
        const mc=bt.getComponent(MovieClip)
        mc.gotoAndStop(frame)
    }
    private onPreClose():void{
        this.onClose()
        this.closeCallback&&this.closeCallback()
    }
}